function mouseover(element)
{
    let mouseOverEvent = new MouseEvent("mouseover", {
        view: window,
        bubbles: true,
        cancelable: true
    });
    // 触发事件
    element.dispatchEvent(mouseOverEvent);
};


function query_element_is_hash(csspath)
{
    elements = document.querySelectorAll(csspath);
    element_length = elements.length;
    if(element_length == 0)
    {
        return false;
    }
    else
    {
        return true;
    }

}

function query_element_is_only(csspath)
{
    elements = document.querySelectorAll(csspath);
    element_length = elements.length;
    if(element_length == 1)
    {
        return true;
    }
    else
    {
        return false;
    }

}


// 定义一个等待函数
async function wait(ms)
{
    return new Promise(resolve => setTimeout(resolve, ms));
}


async function set_input_value(element,value)
{
    let dom = element;
    let lastValue = dom.value;
    dom.value = value;
    var event = document.createEvent("HTMLEvents");
    event.initEvent("input", true, true);
    let tracker = dom._valueTracker
    if (tracker) {
        tracker.setValue(lastValue)
    }
    dom.dispatchEvent(event);
}



async function 启动_选择图库_悬浮效果()
{
    let element = document.querySelector("input[type='file'] ~ div div[class='styles_firstImg__lRdHL'] ");
    mouseover(element);
}


async function 进入_图库控件()
{
    let elements = document.querySelectorAll("svg + div[class='index-module_text__Bi8kU']");
    for (let index = 0; index < elements.length; index++)
    {
        if(elements[index].innerText=='图库选择')
        {
            console.log(elements[index]);
            element = elements[index];
            break;
        }
    }
    element.click();
}

async function 打开隐藏不可用图片()
{
    // div.action-d4c4b3 label.auxo-checkbox-wrapper span
    let result = query_element_is_only("iframe[title='素材中心']");
    if(result)
    {
        let iframe_element = document.querySelector("iframe[title='素材中心']");
        let iframe_document = iframe_element.contentDocument;
        
        let element = iframe_document.querySelector("div.action-d4c4b3 label.auxo-checkbox-wrapper span");
        if(!element.classList.contains("auxo-checkbox-checked"))
        {
            element.click();
        }
        else
        {
            return;
        }
    }
    else
    {
        alert("未找到 iframe 文档");
        return;
    }
}



async function 收缩_图片空间列表()
{
    let result = query_element_is_only("iframe[title='素材中心']");
    if(result)
    {
        let iframe_element = document.querySelector("iframe[title='素材中心']");
        let iframe_document = iframe_element.contentDocument;
        
        iframe_document.querySelector("div span[title='飞鸽图']").click();
        await wait(500);
        iframe_document.querySelector("div span[title='飞鸽机器人图']").click();
        await wait(500);

        iframe_document.querySelector("div span[title='商品资质图片']").click();
        await wait(500);

        iframe_document.querySelector("div span[title='开放平台']").click();
        await wait(500);

        iframe_document.querySelector("div span[title='抖店智能助手']").click();
        await wait(500);        
    }
    else
    {
        alert("未找到 iframe 文档");
        return;
    }
}


async function 展开_一级目录(directory_name)
{
    let result = query_element_is_only("iframe[title='素材中心']");
    if(result)
    {
        let iframe_element = document.querySelector("iframe[title='素材中心']");
        let iframe_document = iframe_element.contentDocument;
        let element = iframe_document.querySelector(`span[title='${directory_name}'].auxo-tree-node-content-wrapper`);
        if(element.classList.contains("auxo-tree-node-content-wrapper-close"))
        {
            element.click();
        }
    }
}

async function 展开_二级目录(directory_name)
{
    let result = query_element_is_only("iframe[title='素材中心']");
    if(result)
    {
        console.log("找到了 iframe");
        let iframe_element = document.querySelector("iframe[title='素材中心']");
        let iframe_document = iframe_element.contentDocument;

        let element = iframe_document.querySelector(`span[title='${directory_name}'].auxo-tree-node-content-wrapper span`);
        console.log(element);
        if(element)
        {
            console.log("clicked");
            element.click();
        }
    }
}



async function 获取所有的文件名()
{
    let result = query_element_is_only("iframe[title='素材中心']");
    if(result)
    {
        let iframe_element = document.querySelector("iframe[title='素材中心']");
        let iframe_document = iframe_element.contentDocument;

        let image_panes = iframe_document.querySelectorAll("div.fileCard-cbadae");
        let image_ns = image_panes.length;

        console.log(`当前文件夹中的图片数量有 ${image_ns} 张!`);

        let elements = iframe_document.querySelectorAll("div.fileCard-cbadae");
        let image_boxs = Array();
        for (let index = 0; index < elements.length; index++) {
            let element = elements[index];
            image_boxs.push(element);
        }
        return image_boxs;
    }
}



async function 对主图图片数组进行排序过滤并点击(elements_array) {
    // 过滤掉包含 "尺"、"码"、"表"、"详"、"情" 任一字符的元素
    elements_array = elements_array.filter(
        function (element) {
            return !element.innerText.includes("尺") &&
                   !element.innerText.includes("码") &&
                   !element.innerText.includes("表") &&
                   !element.innerText.includes("详") &&
                   !element.innerText.includes("情");
        }
    );
    console.log(elements_array);

    // 对过滤后的数组进行排序
    elements_array.sort(
        function (element_a, element_b) {
            let aText = element_a.querySelector("div.fileNameText-_3b9f2").innerText;
            let bText = element_b.querySelector("div.fileNameText-_3b9f2").innerText;
            let aNum = parseInt(aText, 10);
            let bNum = parseInt(bText, 10);
            return aNum - bNum; // 升序排序
        }
    );
    console.log(elements_array);

    // 对前五个元素进行点击,五个点击完毕会自动的进行跳转.
    for (let index = 0; index < 5; index++)
    {
        let element = elements_array[index];
        element.querySelector("div.imgDefault-_99495").click();
        await wait(200);
    }
}



async function 结束_确认()
{
    elements = document.querySelectorAll("button.ecom-g-btn span");
    for (let index = 0; index < elements.length; index++) {
        const element = elements[index];
        if(element.innerText === "确定")
        {
            element.click();
            break;
        }
    }
}

async function 结束_取消()
{
    elements = document.querySelectorAll("button.ecom-g-btn span");
    for (let index = 0; index < elements.length; index++) {
        const element = elements[index];
        if(element.innerText === "取消")
        {
            element.click();
            break;
        }
    }
}




(
    async function()
    {
        结束_取消();


        

        // 1.控制某个元素的悬浮效果启动.
        await 启动_选择图库_悬浮效果();
        await wait(2000);



        // 2.点击进入图库控件.
        await 进入_图库控件();
        await wait(2000);


        // 3.打开不可用图片隐藏
        await 打开隐藏不可用图片();


        // 4.进入指定目录
        await 收缩_图片空间列表();
        await wait(2000);
            
        
        // 5.展开一级目录
        await 展开_一级目录("T恤");
        await wait(2000);


        // 6.展开二级目录
        await 展开_二级目录(directory_name="T-01-003");
        await wait(2000);

        // 7.获取所有图片文件内容
        let images_array = await 获取所有的文件名();
        await wait(2000);


        // 8.对获取到的图片进行过滤和排序
        await 对主图图片数组进行排序过滤并点击(images_array);
        await wait(2000);


        // 9.确定主图图片
        await 结束_确认();
        await wait(2000);


        // 10.填写标题
        title_element = document.querySelector("input[placeholder='请输入2-60个字符（1-30个汉字）']");
        title_value = "男装T恤帅气短袖上衣"
        await set_input_value(title_element,title_value);
        
        
        

    }
)();